package com.game.ThauanFramework;

import Enums.AnimationType;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;

public class Sprite {
	private static final String TAG = "SPRITE";

	private int frameNr;
	private int currentFrame;
	private double framePeriod;

	private float degrees = 0;
	private Bitmap bitmap;
	private Rect selectRect;

	public Rect getSelectRect() {
		return selectRect;
	}

	private int spriteWidth;
	private int spriteHeight;

	private AnimationType type;

	private double cronometro;

	private Paint paint;

	private boolean pause = false;
	private boolean finished = false;

	public Sprite() {
		paint = new Paint();
	}

	public void setSaturation(float sat) {
		Util.Paint_ChangeSat(paint, sat);
	}

	public void setOpacity(int alpha) {
		this.paint.setAlpha(alpha);
	}

	public Bitmap rotatedBitmap(Bitmap source, float degrees) {
		Matrix matrix = new Matrix();
		this.degrees = degrees;
		matrix.postRotate(degrees);
		
		Bitmap rotatedBitmap = Bitmap
				.createBitmap(source, 0, 0, source.getWidth(),
						source.getHeight(), matrix, true);
		
		return rotatedBitmap;
	}
	public void Rotate(float degrees)
	{
		this.bitmap = rotatedBitmap(this.bitmap, degrees);
	}
	public void Start(SpriteAnimationData spriteAnimation) {

		this.frameNr = spriteAnimation.frameNr;
		this.spriteWidth = spriteAnimation.bitmap.getWidth()
				/ spriteAnimation.frameNr;
		this.spriteHeight = spriteAnimation.bitmap.getHeight();
		this.framePeriod = (double) 1 / spriteAnimation.FPS;
		// Log.i(TAG, "frame period: " + framePeriod);

		this.currentFrame = 0;
		this.selectRect = new Rect(0, 0, spriteWidth, spriteHeight);
		this.bitmap = spriteAnimation.bitmap;
		this.type = spriteAnimation.type;
		this.cronometro = 0;
		this.currentFrame = 0;

		this.finished = false;

	}

	public void ChangeSprite(SpriteAnimationData spriteAnimation) {
		this.frameNr = spriteAnimation.frameNr;
		this.spriteWidth = spriteAnimation.bitmap.getWidth()
				/ spriteAnimation.frameNr;
		this.spriteHeight = spriteAnimation.bitmap.getHeight();
		this.framePeriod = 1 / spriteAnimation.FPS;
		this.currentFrame = 0;
		this.selectRect = new Rect(0, 0, spriteWidth, spriteHeight);
		this.bitmap = spriteAnimation.bitmap;
		this.type = spriteAnimation.type;

		this.cronometro = 0;
		this.currentFrame = 0;

		this.finished = false;
	}

	public void Update(double deltaTime) {

		if (!pause || !finished) {

			this.cronometro += deltaTime;
			// Log.i(TAG, "Sprite Update  " + this.cronometro);
			if (cronometro >= framePeriod) {
				this.cronometro = 0;

				if (currentFrame >= frameNr - 1) {
					if (this.type.equals(AnimationType.ONCE)) {
						onSpriteAnimationFinished();

					} else {
						currentFrame = 0;
					}
				} else {
					currentFrame++;
				}

				this.selectRect.left = this.currentFrame * this.spriteWidth;
				this.selectRect.right = this.selectRect.left + this.spriteWidth;

			}
		}
	}

	public void Draw(Canvas canvas, Rect destRect) {
		canvas.drawBitmap(this.bitmap, this.selectRect, destRect, paint);
	}

	// $$ GETTERS AND SETTERS $$
	public boolean isFinished() {
		return finished;
	}

	public void onSpriteAnimationFinished() {
		finished = true;

	}

	public void Pause() {
		this.pause = true;
	}

	public void Resume() {
		this.pause = false;
	}

	public int getFrameNr() {
		return frameNr;
	}

	public void setFrameNr(int frameNr) {
		this.frameNr = frameNr;
	}

	public int getCurrentFrame() {
		return currentFrame;
	}

	public void setCurrentFrame(int currentFrame) {
		this.currentFrame = currentFrame;
	}

	public double getFramePeriod() {
		return framePeriod;
	}

	public void setFramePeriod(double framePeriod) {
		this.framePeriod = framePeriod;
	}

	public Bitmap getBitmap() {
		return bitmap;
	}

	public void setBitmap(Bitmap bitmap) {
		this.bitmap = bitmap;
	}

	public AnimationType getType() {
		return type;
	}

	public void setType(AnimationType type) {
		this.type = type;
	}

	public void setSelectRect(Rect selectRect) {
		this.selectRect = selectRect;
	}

	public void setFinished(boolean finished) {
		this.finished = finished;
	}

	public int getSpriteWidth() {
		return spriteWidth;
	}

	public void setSpriteWidth(int spriteWidth) {
		this.spriteWidth = spriteWidth;
	}

	public int getSpriteHeight() {
		return spriteHeight;
	}

	public void setSpriteHeight(int spriteHeight) {
		this.spriteHeight = spriteHeight;
	}

}
